package com.itcam;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class MinIOApplication {
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException {

        try {
            //构造MinIO Client
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://192.168.150.101:9000")
                    .credentials("minio0920", "minio0920")
                    .build();

            // 查找桶
            boolean found = minioClient.bucketExists(
                    BucketExistsArgs.builder()
                            .bucket("minio0920")
                            .build());
            if (!found) {
                // 创建桶
                minioClient.makeBucket(
                        MakeBucketArgs.builder()
                                .bucket("minio0920")
                                .build());
                //设置CammyBuckets桶的访问权限
                String policy = "{\n" +
                        "    \"Version\": \"2012-10-17\",\n" +
                        "    \"Statement\": [\n" +
                        "        {\n" +
                        "            \"Effect\": \"Allow\",\n" +
                        "            \"Principal\": \"*\",\n" +
                        "            \"Action\": [\n" +
                        "                \"s3:GetBucketLocation\",\n" +
                        "                \"s3:ListBucket\"\n" +
                        "            ],\n" +
                        "            \"Resource\": \"arn:aws:s3:::minio0920\"\n" +
                        "        },\n" +
                        "        {\n" +
                        "            \"Effect\": \"Allow\",\n" +
                        "            \"Principal\": \"*\",\n" +
                        "            \"Action\": \"s3:GetObject\",\n" +
                        "            \"Resource\": \"arn:aws:s3:::minio0920/*\"\n" +
                        "        }\n" +
                        "    ]\n" +
                        "}";
                minioClient.setBucketPolicy(
                        SetBucketPolicyArgs.builder()
                                .bucket("minio0920")
                                .config(policy)
                                .build());

                // 上传图片
                minioClient.uploadObject(
                        UploadObjectArgs.builder().bucket("minio0920")
                                .object("girl.jpg")
                                .filename("C:\\Users\\Cammy\\Pictures\\Saved Pictures\\Girl.png")
                                .build()
                );
                System.out.println("successfully!");
            }
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }
}
